<template>
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">
        <b-card header="<i class='fa fa-align-justify'></i> 方案列表">
          <!--<div class="search-box">-->
            <!--<input type="text" class="form-control"  placeholder="goodsid" v-model="companyName">-->
            <!--<b-button variant="outline-primary" @click="searchRif()">搜索</b-button>-->
          <!--</div>-->
          <div class="search-box">
            <b-button variant="outline-primary" @click="addSolPro()">添加产品</b-button>
          </div>
          <div class="table-box">
            <table class="table table-striped">
              <thead>
              <tr>
                <th>序号</th>
                <th>产品名</th>
                <th>产品ID</th>
                <th>分类名</th>
                <th>分类ID</th>
                <th>图片</th>
                <th>更新人</th>
                <th>更新时间</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td colspan="9" v-show="isSolList" style="text-align: center;">没有搜索到匹配的信息!</td>
              </tr>
              <tr  v-for="(item, index) in currentData">
                <td>{{ index + 1 + number}}</td>
                <td>{{ item.name }}</td>
                <td>{{ item.goodsid }}</td>
                <td>{{ item.class2name }}</td>
                <td>{{ item.class2 }}</td>
                <td>{{ item.image }}</td>
                <td>{{ item.updateUser }}</td>
                <td>{{ item.createtime }}</td>
                <td>
                  <!--<span class="badge badge-success" @click="addSolList(item.sid)">添加</span>-->
                  <span class="badge badge-danger" @click="deleteSolList(item.id)">删除</span>
                </td>
              </tr>
              </tbody>
            </table>
          </div>

          <el-pagination
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :current-page.sync="current"
            :page-size="10"
            :total="total">
          </el-pagination>

        </b-card>
      </div>
    </div>

    <div title="Modal title" class="modal-primary" v-model="primaryModal" v-show="primaryModal">
      <span class="modal-open">
        <div class="modal fade show" role="dialog">
          <div class="modal-dialog modal-md">
            <div tabindex="-1" role="document" class="modal-content">
              <header class="modal-header">
                <h5 class="modal-title">删除解决方案</h5>
                <button type="button" aria-label="Close" class="close" @click="cancelModal()">
                  <span aria-hidden="true">×</span>
                </button>
              </header>
              <div class="modal-body">
                <br>是否删除该条方案信息？<br><br>
              </div>
              <footer class="modal-footer">
                <button type="button" class="btn btn-primary btn-md" @click="cancelModal()">取消</button>
                <button type="button" class="btn btn-danger btn-md" @click="sureDeleteSol()">确认</button>
              </footer>
            </div>
          </div>
        </div>
        <div class="modal-backdrop fade show"></div>
      </span>
    </div>

  </div>

</template>

<script>
  import { findIdByProduct, deleteProduct } from '../../service/api'
  import _ from 'lodash'
  export default {
    name: 'tables',
    data () {
      return {
        current: 1,
        number: 0,
        rfiList: [],
        pageNumber: '',
        companyName: '',
        isSolList: false,
        primaryModal: false,
        delId: '',
        updateName: ''
      }
    },
    computed: {
      currentData () {
        return this.rfiList[this.current - 1]
      },
      total () {
        return ~~this.pageNumber
      }
    },
    methods: {
      // 关闭浮层
      cancelModal () {
        this.primaryModal = false
      },
      // 查看该条解决方案下的子内容
      addSolList (index) {
        this.$router.push({
          path: '/solution/s_l_editDetail?id=' + index
        })
      },
      // 打开删除浮层
      deleteSolList (index) {
        this.delId = index
        this.primaryModal = true
      },
      // 确认
      sureDeleteSol () {
        // alert(this.delId)
        console.log(this.delId)
        deleteProduct({id: this.delId})
          .then(({ data, err }) => {
            console.log('data: ' + JSON.stringify(data))
            console.log('err: ' + err)
            if (err || data.code !== 200) {
              alert('删除失败！')
              return false
            }
            this.primaryModal = false
            alert('删除成功！')
            location.reload()
          })
      },
      addSolPro () {
        this.$router.push({
          path: '/solution/s_l_editDetail?sid=' + this.$route.query.id
        })
      },
      // 搜索
      handleCurrentChange () {
        console.log('handleCurrentChange: ' + this.current)
        if (this.current === 1) {
          this.number = 0
        } else {
          this.number = (this.current - 1) * 10
        }
      }
    },
    created () {
      // fetch
      this.updateName = localStorage.getItem('username')
      console.log(this.$route.query.id)
      findIdByProduct({id: this.$route.query.id})
        .then(({ data, err }) => {
          console.log('err: ' + err)
          console.log('data: ' + data)
          if (data.code === 200) {
            this.pageNumber = data.result.length
            this.rfiList = _.chunk(data.result, 10)
            if (data.result.length === 0) {
              this.isSolList = true
            } else {
              this.isSolList = false
            }
          }
        })
    }
  }
</script>
<style type="text/scss" rel="stylesheet/scss" lang="scss" >
  @import "../../styles/common";
  /*.badge{*/
    /*cursor: pointer;*/
  /*}*/
  /*.search-box{*/
    /*vertical-align: top;*/
    /*margin-bottom: 20px;*/
  /*}*/
  /*.search-box input{*/
    /*vertical-align: top;*/
    /*display: inline-block;*/
    /*width: 250px;*/
  /*}*/
</style>
